package com.gosophia.govDoc.action.transientDoc;

import java.util.Collections;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.gosophia.commons.web.action.BaseSupportAction;
import com.gosophia.dataDictionary.dto.DataValueDTO;
import com.gosophia.dataDictionary.ws.DataDictionaryWebService;
import com.gosophia.govDoc.dto.TransientDocumentDTO;
import com.gosophia.govDoc.ws.TransientDocumentWebService;
import com.gosophia.springsecurity.UserDetailsImpl;

/**
 * 文档起草渲染Action
 * 
 * @author zhanghuanchao
 * @since 2011-01-27
 * 
 */
@Controller("CreateDocumentInputPageAction")
@Scope("prototype")
@SuppressWarnings("serial")
public class CreateDocumentInputPageAction extends BaseSupportAction {

    private List<DataValueDTO> dataValueList;

    private String approvalPersonString;

    private TransientDocumentDTO transientDocumentDto;

    @Autowired
    private DataDictionaryWebService dataDictionaryWebService;
    @Autowired
    private TransientDocumentWebService transientDocumentWebService;

    private boolean hasTransientDoc;

    private final Logger logger = Logger.getLogger(this.getClass());

    /**
     * 从业务进入 文档起草第一个页面
     * 
     * @return
     */
    public String importantLevelInputPage() {
        UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder
                .getContext().getAuthentication().getPrincipal();
        // 得到当前用户
        Long creatorId = userDetails.getUserAccountDTO().getUserInfoDTO()
                .getUserInfoId();
        Long jobId = transientDocumentDto == null ? null : transientDocumentDto
                .getJobId();
        logger.debug("公文起草进入Action");
        transientDocumentDto = transientDocumentWebService
                .findTranDocByCreatorAndJobId(creatorId, jobId);
        if (transientDocumentDto == null) {
            hasTransientDoc = false;
        } else {
            hasTransientDoc = true;
        }
        dataValueList = dataDictionaryWebService
                .findEnabledDataValueByEntryName("zhongyaochengdu");// 任务重要程度
        Collections.reverse(dataValueList);
        return SUCCESS;
    }

    /**
     * 从业务进入 文档起草第二个页面
     * 
     * @return
     */
    public String documentTypeInputPage() {
        UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder
                .getContext().getAuthentication().getPrincipal();
        // 得到当前用户
        Long creatorId = userDetails.getUserAccountDTO().getUserInfoDTO()
                .getUserInfoId();
        Long jobId = transientDocumentDto == null ? null : transientDocumentDto
                .getJobId();
        transientDocumentDto = transientDocumentWebService
                .findTranDocByCreatorAndJobId(creatorId, jobId);
        return SUCCESS;
    }

    public List<DataValueDTO> getDataValueList() {
        return dataValueList;
    }

    public void setDataValueList(List<DataValueDTO> dataValueList) {
        this.dataValueList = dataValueList;
    }

    public String getApprovalPersonString() {
        return approvalPersonString;
    }

    public void setApprovalPersonString(String approvalPersonString) {
        this.approvalPersonString = approvalPersonString;
    }

    public TransientDocumentDTO getTransientDocumentDto() {
        return transientDocumentDto;
    }

    public void setTransientDocumentDto(
            TransientDocumentDTO transientDocumentDto) {
        this.transientDocumentDto = transientDocumentDto;
    }

    @Override
    public String _execute() throws Exception {
        return null;
    }

    public boolean isHasTransientDoc() {
        return hasTransientDoc;
    }

    public void setHasTransientDoc(boolean hasTransientDoc) {
        this.hasTransientDoc = hasTransientDoc;
    }

}
